'''
Author: your name
Date: 2021-03-23 10:07:52
LastEditTime: 2021-03-23 10:57:47
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: /tinyms/tinyms-api/apps/auth/role/service.py
'''
import json
from app import db
from .model import Role
from datetime import datetime,timedelta


class RoleService():
    def pages(page):
        pages = Role.query.paginate(int(page),30,False) #flask_sqlalchemy提供分页方法.paginate
        # pjson = json.dumps(pages, ensure_ascii=False,default=lambda o: o.__dict__)
        return pages
    
    def list():
        roles = Role.query.filter().all()
        return roles
    
    def save(role):
        db.session.add(role)
        db.session.commit()
    
    def update(role_pk,role_status):
        role = Role.query.get(role_pk)
        role.role_status = role_status
        db.session.commit()
    
    #根据主键删除数据
    def delete(role_pk):
        role = Role.query.get(role_pk)
        db.session.delete(role)
        db.session.commit()#必须要有db.session.commit,否则数据无法删除